// page/tabBar/bindphone/bindphone.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oneKeyBinding: true,
    inputPhoneNumber:'',
    inputVerifyCode:''
  },

  // 切换
  switch(){
    const that = this;
    that.setData({
      oneKeyBinding: !that.data.oneKeyBinding
    })
  },

  // 获取表单手机号
  getInputPhoneNumber(e){
    const that = this;
    that.setData({
      inputPhoneNumber: e.detail.value
    })
  },

  // 获取表单验证码
  getInputVerifyCode(e) {
    const that = this;
    that.setData({
      inputVerifyCode: e.detail.value
    })
  },

  //验证手机号获取验证码
  getVerifyCode() {
    const that = this;
    let token = app.globalData.token;
    let json = {
      scene: 6,
      unique_id: token,
      mobile: that.data.inputPhoneNumber
    }
    let jsonData = app.utils.getSignData(json);
    app.utils.post(app.api.reqUrls.phone_code, jsonData).then(res => {
      if (res.status == 1) {
        app.showInfo(res.msg)
      } else {
        throw res.msg;
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      if (err != undefined) {
        app.showInfo(err.toString())
      }else{
        console.log('服务器错误')
      }
    })
  },

  // 输入验证码点击确认
  clickConfirm() {
    const that = this;

    let token = app.globalData.token;
    let json = {
      token,
      scene:6,
      check_code: that.data.inputVerifyCode,
      unique_id: token,
      mobile: that.data.inputPhoneNumber
    }
    let jsonData = app.utils.getSignData(json);
    that.setData({
      hiddenLoading: false
    })
    app.utils.post(app.api.reqUrls.bindPhone, jsonData).then(res => {
      console.log(res)
      that.setData({
        hiddenLoading: true
      })
      if (res.status == 1) {
        if (res.result.token) {
          app.globalData.token = res.result.token;
          wx.setStorageSync("token", res.result.token);
        }
        wx.showModal({
          content: '手机号绑定成功',
          showCancel: false,
          confirmText: '我知道了',
          confirmColor: '#d81e06',
          success(e) {
            if (e.confirm) {
              app.globalData.isfirstnewuser = true;
              wx.switchTab({
                url: '/page/tabBar/my/my',
              })
            }
          }
        })
      } else {
        throw res.msg
      }
    }).catch(err => {
      that.setData({
        hiddenLoading: true
      })
      if (err != undefined) {
        app.showInfo(err.toString())
      } else {
        console.log('服务器错误')
      }
    })

  },

  // 获取微信手机号
  getPhoneNumber(e) {
    const that = this;
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      let token = app.globalData.token;
      let json = {
        token,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        code: that.data.code
      }
      let jsonData = app.utils.getSignData(json);
      that.setData({
        hiddenLoading: false
      })
      app.utils.post(app.api.reqUrls.getMiniPhone, jsonData).then(res => {
        that.setData({
          hiddenLoading: true
        })
        if (res.status == 1) {
          if (res.result.token) {
            app.globalData.token = res.result.token;
            wx.setStorageSync("token", res.result.token);
          }
          wx.showModal({
            content: '手机号绑定成功',
            showCancel: false,
            confirmText:'我知道了',
            confirmColor:'#d81e06',
            success(e){
              if(e.confirm){
                app.globalData.isfirstnewuser = true;
                wx.switchTab({
                  url: '/page/tabBar/my/my',
                })
              }
            }
          })
        } else {
          throw res.msg
        }
      }).catch(err => {
        that.setData({
          hiddenLoading: true
        })
        if (err != undefined) {
          app.showInfo(err.toString())
        } else {
          console.log('服务器错误')
        }
      })
    } else {
      return false;
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    wx.login({
      success: function (res) {
        console.log(res)
        if (res.errMsg == "login:ok") {
          that.setData({
            code: res.code
          })
        }
      }
    })
  }
})